<?php
if (!defined('CUSTOM_YOUTUBI_VERSION')) {
    exit ('Not allow');
}

add_action('wp_head', 'custom_youtubi_css');
function custom_youtubi_css() {
    echo '<link rel="stylesheet" href="'. WP_PLUGIN_URL . '/' . CUSTOM_YOUTUBI_BASEFOLDER . '/custom-youtubi.css" type="text/css" media="screen" />';
}

add_filter('the_content', 'get_custom_youtubi');
function get_custom_youtubi($output) {
    global $id, $post;
    global $youtubi_config;

    $flash_pattern = '<object width="%2$d" height="%3$d">
            <param name="movie" value="%1$s&fs=1&"></param>
            <param name="allowFullScreen" value="true"></param>
            <param name="allowscriptaccess" value="always"></param>
            <embed src="%1$s&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="%2$d" height="%3$d"></embed>
        </object>';
    $image_pattern = 'http://i4.ytimg.com/vi/%1$s/%2$s.jpg';

    $youtubi_id = get_post_meta($id, 'youtubi', true);

    if (!$youtubi_id){
        return $output;
    } else {
        $youtubi_info = array(
            'flash' => sprintf(
                    $flash_pattern,
                    'http://www.youtube.com/v/' . $youtubi_id,
                    $youtubi_config['flashWidth'],
                    $youtubi_config['flashHeight']
                    ),
            'image' => sprintf($image_pattern, $youtubi_id, $youtubi_config['showImage']),
        );

        if (is_singular()) {
            $output = $output . '<p class="custom-youtubi-flash">' . $youtubi_info['flash'] . '</p>';
        } else {
            $output = '<p class="custom-youtubi-image"><img src="' . $youtubi_info['image'] . '" alt="'. $post->post_title .'" /></p>' . $output;
        }

        return $output;
    }
}

/* EOF get-youtubi.php */
/* ./wp-content/plugins/custom-youtubi/get-youtubi.php */